// 导入koa-router模块
const Router = require('koa-router');
// 创建koa-router的实例router
const router = new Router();
const weatherControllers = require('../controllers/weather');
// 定义模型 可以公用 schema $ref
/**
 * @swagger
 * definitions:
 *   Login:
 *     required:
 *       - username
 *       - password
 *     properties:
 *       username:
 *         type: string
 *       password:
 *         type: string
 *       path:
 *         type: string
 */


//  tags 可以理解成借口分类  parameters 参数
/**
 * @swagger
 * /login:
 *   post:
 *     description: 用户登入
 *     tags: [用户登入模块]
 *     produces:
 *       - application/json
 *     parameters:
 *       - name: password
 *         description: 用户密码.
 *         in: formData
 *         required: true
 *         type: string
 *       - name: username
 *         description: 用户名.
 *         in: formData
 *         required: true
 *         type: string
 *     responses:
 *       200:
 *         description: 登入成功
 *         schema:
 *           type: object
 *           $ref: '#/definitions/Login'
 *   
 */

router.get('/ssr', async ctx => {
    // 第 1 步：创建一个 Vue 实例
    // let weather = await weatherControllers.getWeatherNow(ctx)
    // ctx.state.weather = weather
    // console.log(weather, 'weather');
    const Vue = require('vue')
    const app = new Vue({
        template: `<div>Hello World</div>`
    })
    // 第 2 步：创建一个 renderer
    const renderer = require('vue-server-renderer').createRenderer();
    // 第 3 步：将 Vue 实例渲染为 HTML
    renderer.renderToString(app, (err, html) => {
        if (err) throw err
        console.log(html)
        ctx.body = html;
        // => <div data-server-rendered="true">Hello World</div>
    })
})
router.get('/get', ctx => {
    ctx.body = "哈哈哈2"
})
router.post('/post', ctx => {
    // ctx.body = "哈哈哈3"
    ctx.body = ctx.request.body
})
router.all('/all', ctx => {
    ctx.body = "哈哈哈4"
})
router.get("/html", async ctx => {
    await ctx.render("tpl", {
        title: "Grayly"
    })
})

module.exports = router